package PlotPourri;

import java.util.ArrayList;
import java.util.List;

public class Band extends ArrayList<Observation> {

    private BandName name;
    private int order;

    public Band() {
    }

    public Band(BandName name) {
        this.name = name;
    }

    public Band(BandName name, int size) {
        super(size);
        this.name = name;
    }

    @Override
    public boolean add(Observation file) {
        file.setBand(getName());
        return super.add(file);
    }

    public List<double[][]> getPlots() {
        List<double[][]> plots = new ArrayList<double[][]>();
        for (Observation file : this) {
            double[][] tmp = new double[file.size()][2];
            for (int j = 0; j < file.size(); j++) tmp[j] = new double[] {file.getPhase(j), file.getMagnitude(j)};
            plots.add(tmp);
        }
        return plots;
    }

    public Observation combine() {
        Observation combine = new Observation();
        for (int i = 0; i < this.size(); i++) {
            Observation file = this.get(i);
            for (double[] list : file) combine.add(list);
            if (i == 0) {
                combine.setPeriod(file.getPeriod());
                combine.setPhasePosition(file.getPhasePosition());
                combine.setMagnitudePosition(file.getMagnitudePosition());
                combine.setErrorPosition(file.getErrorPosition());
                combine.setJulianDatePosition(file.getJulianDatePosition());
                combine.setReddening(file.getReddening());
            }
        }
        return combine;
    }

    public int getOrder(double dispersionTolerance, int xsize, int maxOrder) {
        double previous = Double.POSITIVE_INFINITY;
        int tmpord = 1;
        for (; tmpord < maxOrder; tmpord++) {
            double disp = 0;
            if (size() == 1) disp = get(0).getDisp(tmpord, xsize, dispersionTolerance);
            //else // fix me!
            if (disp >= previous) break;
            previous = disp;
        }
        return tmpord;
    }

    public Band getOrderedBand(double tolerance) {
        Band correct = new Band();
        Observation leastList = null;
        double leastDisp = Double.POSITIVE_INFINITY;
        int bestOrder = 1;
        for (Observation file : this) {
            double previous = Double.POSITIVE_INFINITY;
            for (int tmpord = 1; tmpord < 13; tmpord++) {
                double disp = file.getDisp(tmpord);
                if (disp >= previous) break;
                previous = disp;
                if (disp < leastDisp) {
                    leastList = file;
                    leastDisp = disp;
                    bestOrder = tmpord;
                }
            }
        }
        if (leastDisp == Double.POSITIVE_INFINITY) return correct;
        correct.add(leastList);
        for (Observation file : this) {
            if (correct.contains(file)) continue;
            Observation tmp = new Observation();
            for (double[] dbls : file) tmp.add(dbls);
            for (double[] dbls : leastList) tmp.add(dbls);
            //double disp = tmp.getDisp(bestOrder, oscThresh);
            double disp = tmp.getDisp(bestOrder);
            if (disp < leastDisp * tolerance) {
                correct.add(file);
                leastDisp = disp;
            }
        }
        correct.setOrder((bestOrder < Double.POSITIVE_INFINITY) ? bestOrder : 1);
        return correct;
    }

    public BandName getName() {
        return name;
    }

    public void setName(BandName name) {
        this.name = name;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }
}
